#!/bin/bash
#
# This script updates my local debian repository
#

set -e

export LC_ALL=C
export LANG=C

__warn() {
	if [ ! -z "$1" ]; then
		echo "$1" >&2
	fi
}

__die() {
	__warn "$1"
	exit 1
}

__debug() {
	if [ $# -lt 2 ]; then
		if [ ! -z "$__DEBUG" ]; then
			__warn "$1"
		fi
	else
		if [ $1 -ge $__DEBUG ]; then
			__warn "$2"
		fi
	fi
}

parseCmdline() {
	while [ $# -gt 0 ]
	do
		PARAM="$1"
		case "$PARAM" in
			--upload-only)
				__UPLOAD_ONLY="yes"
				shift
				;;
			--debug)
				if [ $# -gt 1 ]; then
					__DEBUG="$2"
					shift
				else
					__DEBUG=1
				fi
				;;
			*)
				__die "Incorrect parameter supplied: $PARAM"
				;;
		esac
	done
}

fixPermissions() {
	find ${APT_REPO_PREFIX} -type f -print0 | xargs -r0 chmod 644
	find ${APT_REPO_PREFIX} -type d -print0 | xargs -r0 chmod 755
}

uploadRepository() {
	fixPermissions

	# Check if script run in interactive mode
	if [ -t 0 -o -p /dev/stdin ]; then
		rsync -aPz --delete --delete-after ${APT_REPO_PREFIX}/pool repository:/var/vhosts/tataranovich.com/debian/ && \
		rsync -aPz --delete --delete-after --exclude='*.db' --delete-excluded ${APT_REPO_PREFIX}/ repository:/var/vhosts/tataranovich.com/debian/
	else
		rsync -az --partial --delete-after ${APT_REPO_PREFIX}/pool repository:/var/vhosts/tataranovich.com/debian/ && \
                rsync -az --delete --delete-after --exclude='*.db' --delete-excluded ${APT_REPO_PREFIX}/ repository:/var/vhosts/tataranovich.com/debian/
	fi
}

parseCmdline $@

if [ -r ~/bin/prepare-environment.sh ]; then
	. ~/bin/prepare-environment.sh
fi

APT_COMMON_CONF="$HOME/.apt-ftparchive-common.conf"
APT_SQUEEZE_CONF="$HOME/.apt-ftparchive-squeeze.conf"
APT_WHEEZY_CONF="$HOME/.apt-ftparchive-wheezy.conf"
APT_JESSIE_CONF="$HOME/.apt-ftparchive-jessie.conf"
APT_STRETCH_CONF="$HOME/.apt-ftparchive-stretch.conf"
APT_SID_CONF="$HOME/.apt-ftparchive-sid.conf"
APT_LUCID_CONF="$HOME/.apt-ftparchive-lucid.conf"
APT_PRECISE_CONF="$HOME/.apt-ftparchive-precise.conf"
APT_TRUSTY_CONF="$HOME/.apt-ftparchive-trusty.conf"
APT_UTOPIC_CONF="$HOME/.apt-ftparchive-utopic.conf"
APT_VIVID_CONF="$HOME/.apt-ftparchive-vivid.conf"
APT_REPO_PREFIX="$HOME/my-local-repo"

if [ "$__UPLOAD_ONLY" == "yes" ]; then
	__debug "Only syncing local and remote repository"
	uploadRepository
	exit $?
fi

if [ -x ~/bin/cleanup-nightly-builds.sh ]; then
	~/bin/cleanup-nightly-builds.sh
fi

# Currently I didn't know how to use them properly, so I simply
# delete all of them before repo rescan.
rm -f ${APT_REPO_PREFIX}/*.db

# Check if apt-ftparchive available
if [ ! -x `which apt-ftparchive` ]; then
	echo "Seems that apt-ftparchive binary not installed"
	exit 1
fi

apt-ftparchive generate ${APT_COMMON_CONF} 2>&1 | egrep -v 'has no (binary|source) override entry'

if [ -d ${APT_REPO_PREFIX}/dists/squeeze ]; then
	echo "Processing Squeeze release"
	cd ${APT_REPO_PREFIX}/dists/squeeze
	apt-ftparchive -c ${APT_SQUEEZE_CONF} release . > Release
	rm -f Release.gpg
	gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/wheezy ]; then
	echo "Processing Wheezy release"
	cd ${APT_REPO_PREFIX}/dists/wheezy
	apt-ftparchive -c ${APT_WHEEZY_CONF} release . > Release
	rm -f Release.gpg
	gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/jessie ]; then
        echo "Processing Jessie release"
        cd ${APT_REPO_PREFIX}/dists/jessie
        apt-ftparchive -c ${APT_JESSIE_CONF} release . > Release
        rm -f Release.gpg
        gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/stretch ]; then
        echo "Processing Stretch release"
        cd ${APT_REPO_PREFIX}/dists/stretch
        apt-ftparchive -c ${APT_STRETCH_CONF} release . > Release
        rm -f Release.gpg
        gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/sid ]; then
	echo "Processing Sid release"
	cd ${APT_REPO_PREFIX}/dists/sid
	apt-ftparchive -c ${APT_SID_CONF} release . > Release
	rm -f Release.gpg
	gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/lucid ]; then
    echo "Processing Lucid release"
    cd ${APT_REPO_PREFIX}/dists/lucid
    apt-ftparchive -c ${APT_LUCID_CONF} release . > Release
    rm -f Release.gpg
    gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/precise ]; then
    echo "Processing Precise release"
    cd ${APT_REPO_PREFIX}/dists/precise
    apt-ftparchive -c ${APT_PRECISE_CONF} release . > Release
    rm -f Release.gpg
    gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/trusty ]; then
    echo "Processing Trusty release"
    cd ${APT_REPO_PREFIX}/dists/trusty
    apt-ftparchive -c ${APT_TRUSTY_CONF} release . > Release
    rm -f Release.gpg
    gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/utopic ]; then
    echo "Processing Utopic release"
    cd ${APT_REPO_PREFIX}/dists/utopic
    apt-ftparchive -c ${APT_UTOPIC_CONF} release . > Release
    rm -f Release.gpg
    gpg --batch --no-tty --output Release.gpg -ba Release
fi

if [ -d ${APT_REPO_PREFIX}/dists/vivid ]; then
    echo "Processing Vivid release"
    cd ${APT_REPO_PREFIX}/dists/vivid
    apt-ftparchive -c ${APT_VIVID_CONF} release . > Release
    rm -f Release.gpg
    gpg --batch --no-tty --output Release.gpg -ba Release
fi

# Currently I didn't know how to use them properly, so I simply
# delete all of them before repo rescan.
rm -f ${APT_REPO_PREFIX}/*.db

uploadRepository
